package com.cg.service.impl;

import com.cg.mapper.CategoryMapper;
import com.cg.pojo.Category;
import com.cg.pojo.Result;
import com.cg.service.CategoryService;
import com.cg.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public void add(Category category){
            //补充属性值
            category.setCreateTime(LocalDateTime.now());
            category.setUpdateTime(LocalDateTime.now());

            Map<String,Object> map= ThreadLocalUtil.get();
            Integer userId =(Integer) map.get("id");
            category.setCreateUser(userId);
            categoryMapper.add(category);

        }

    @Override
    public List<Category> list() {
       Map<String,Object>map=ThreadLocalUtil.get();
        Integer userId = (Integer)  map.get("id");
        return  categoryMapper.list(userId);
    }
    @Override
    public Category findById(Integer id){
       Category c= categoryMapper.findById(id);
        return c;
    }

    @Override
    public void update(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
    }

    @Override
    public void delete(Integer id){
        categoryMapper.delete(id);
    }
}
